

package com.hazelcast.aggregation;

import com.hazelcast.nio.ObjectDataInput;
import com.hazelcast.nio.ObjectDataOutput;
import com.hazelcast.nio.serialization.DataSerializable;

import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;

public class ValueContainer implements DataSerializable, Comparable<ValueContainer> {

    public ValueType valueType;
    public int intValue;
    public long longValue;
    public float floatValue;
    public double doubleValue;
    public BigDecimal bigDecimal;
    public BigInteger bigInteger;
    public Number numberValue;
    public String stringValue;
    public ValueContainer() {
        this.valueType = ValueType.NUMBER;
    }

    public ValueContainer(int intValue) {
        this.valueType = ValueType.INTEGER;
        this.intValue = intValue;
    }

    public ValueContainer(long longValue) {
        this.valueType = ValueType.LONG;
        this.longValue = longValue;
    }

    public ValueContainer(float floatValue) {
        this.valueType = ValueType.FLOAT;
        this.floatValue = floatValue;
    }

    public ValueContainer(double doubleValue) {
        this.valueType = ValueType.DOUBLE;
        this.doubleValue = doubleValue;
    }

    public ValueContainer(BigDecimal bigDecimal) {
        this.valueType = ValueType.BIG_DECIMAL;
        this.bigDecimal = bigDecimal;
    }

    public ValueContainer(BigInteger bigInteger) {
        this.valueType = ValueType.BIG_INTEGER;
        this.bigInteger = bigInteger;
    }

    public ValueContainer(String stringValue) {
        this.valueType = ValueType.STRING;
        this.stringValue = stringValue;
    }

    @Override
    public void writeData(ObjectDataOutput out) throws IOException {
        out.writeInt(intValue);
        out.writeLong(longValue);
        out.writeFloat(floatValue);
        out.writeDouble(doubleValue);
        out.writeObject(bigDecimal);
        out.writeObject(bigInteger);
        out.writeObject(numberValue);
        out.writeString(stringValue);
    }

    @Override
    public void readData(ObjectDataInput in) throws IOException {
        intValue = in.readInt();
        longValue = in.readLong();
        floatValue = in.readFloat();
        doubleValue = in.readDouble();
        bigDecimal = in.readObject(BigDecimal.class);
        bigInteger = in.readObject(BigInteger.class);
        numberValue = in.readObject(Number.class);
        stringValue = in.readString();
    }

    @Override
    public int compareTo(ValueContainer o) {
        switch (valueType) {
        case INTEGER:
            return Integer.compare(intValue, o.intValue);
        case LONG:
            return Long.compare(longValue, o.longValue);
        case FLOAT:
            return Float.compare(floatValue, o.floatValue);
        case DOUBLE:
            return Double.compare(doubleValue, o.doubleValue);
        case BIG_DECIMAL:
            return bigDecimal.compareTo(o.bigDecimal);
        case BIG_INTEGER:
            return bigInteger.compareTo(o.bigInteger);
        case STRING:
            return stringValue.compareTo(o.stringValue);
        default:
            return 0;
        }
    }

    enum ValueType {
        INTEGER, LONG, FLOAT, DOUBLE, BIG_DECIMAL, BIG_INTEGER, NUMBER, STRING
    }
}
